package com.jike.jkgame.data;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.jike.jkgame.Utilis;
import com.jike.jkgame.db.ResourcesDAO;

public class Treasure {
	public int id;
	public String name;
	public String description;
	public byte max_attack_addon_min;
	public byte max_attack_addon_max;
	public byte min_attack_addon_min;
	public byte min_attack_addon_max;
	public byte max_defense_addon_min;
	public byte max_defense_addon_max;
	public byte min_defense_addon_min;
	public byte min_defense_addon_max;
	public byte max_attack_probability_min;
	public byte max_attack_probability_max;
	public byte max_defense_probability_min;
	public byte max_defense_probability_max;
	public NeededFragment[] needed_fragments;
	
	public static class NeededFragment {
		public int id;
		public byte count;
		
		@Override
		public String toString() {
			return String.format("[%s,%s]", String.valueOf(id), String.valueOf(count));
		}
		
		public static NeededFragment fromString(String str) {
			NeededFragment nf = new NeededFragment();
			if (str.startsWith("[")&& str.endsWith("]")) {
				String subStr = str.substring(1, str.length()-1);
				String[] subs = subStr.split(",");
				
				nf.id = Integer.valueOf(subs[0]);
				nf.count = Byte.valueOf(subs[1]);
			}
			
			return nf;
		}
		
		public static NeededFragment[] stringToArray(String str) {
			if (str == null) {
				return null;
			}
			
			String[] itemStrs = str.split(Utilis.SEPERATOR);
			NeededFragment[] items = new NeededFragment[itemStrs.length];
			for (int i=0; i<itemStrs.length; ++i) {
				items[i] = NeededFragment.fromString(itemStrs[i]);
			}
			
			return items;
		}
	}
	
	private static Treasure parseTreasure(DataInputStream dis, byte[] buff) {
		Treasure treasure = new Treasure();
		
		try {
			treasure.id = dis.readInt();
			treasure.name = Utilis.getString(dis, buff);
			treasure.description = Utilis.getString(dis, buff);
			treasure.max_attack_addon_min = dis.readByte();
			treasure.max_attack_addon_max = dis.readByte();
			treasure.min_attack_addon_min = dis.readByte();
			treasure.min_attack_addon_max = dis.readByte();
			treasure.max_defense_addon_min = dis.readByte();
			treasure.max_defense_addon_max = dis.readByte();
			treasure.min_defense_addon_min = dis.readByte();
			treasure.min_defense_addon_max = dis.readByte();
			treasure.max_attack_probability_min = dis.readByte();
			treasure.max_attack_probability_max = dis.readByte();
			treasure.max_defense_probability_min = dis.readByte();
			treasure.max_defense_probability_max = dis.readByte();
			int cnt = dis.readInt();
			treasure.needed_fragments = new NeededFragment[cnt];
			
			for (int i=0; i<cnt; ++i) {
				NeededFragment nf = new NeededFragment();
				nf.id = dis.readInt();
				nf.count = dis.readByte();
				treasure.needed_fragments[i] = nf;
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		
		return treasure;
	}
	
	public static int parseDataFile(InputStream in) {
		DataInputStream dis = new DataInputStream(in);
		int count  = 0;
		
		ResourcesDAO dao = ResourcesDAO.get();
		byte[] buff = new byte[4096];
		
		try {
			int type = dis.readInt();
			int version = dis.readInt();
			
			int myVersion = dao.getDataFileVersion(type);
			
			if (myVersion >= version) {
				return 0;
			}
			
			dao.clearTreasures();
			Treasure t = null;
			do {
				t = parseTreasure(dis, buff);
				dao.insertTreasure(t);
				count++;
			}while(t != null);
			dao.updateDataFileVersion(type, version);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return count;
		
	}
	
}
